# Start ERP system services
# Note: Please run this script as administrator in PowerShell

Write-Host "Starting ERP system services..." -ForegroundColor Green

# Check and create log directory
$logDir = "$PSScriptRoot\logs"
if (-not (Test-Path $logDir)) {
    New-Item -ItemType Directory -Path $logDir | Out-Null
}

# Start backend Django service
Write-Host "Starting Django backend service..." -ForegroundColor Yellow
Start-Process powershell -WorkingDirectory "$PSScriptRoot\backend" -ArgumentList "python manage.py runserver 0.0.0.0:8000" -RedirectStandardOutput "$logDir\backend.log" -RedirectStandardError "$logDir\backend_error.log" -WindowStyle Maximized

# Wait for backend service to start
Start-Sleep -Seconds 5

# Start frontend Vue service
Write-Host "Starting Vue frontend service..." -ForegroundColor Yellow
Start-Process powershell -WorkingDirectory "$PSScriptRoot\web\apps\web-antd" -ArgumentList "npm run dev" -RedirectStandardOutput "$logDir\frontend.log" -RedirectStandardError "$logDir\frontend_error.log" -WindowStyle Maximized

Write-Host "All services started!" -ForegroundColor Green
Write-Host "Backend URL: http://localhost:8000" -ForegroundColor Cyan
Write-Host "Frontend URL: http://localhost:5678 (may vary based on actual startup)" -ForegroundColor Cyan
Write-Host "Log files saved in: $logDir" -ForegroundColor Cyan
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')